﻿using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Unis.Framework.Core.Exceptions;
using Unis.Framework.Core.Utility;
using ConfigurationManager = System.Configuration.ConfigurationManager;

namespace Unis.Framework.ORM.Xml
{
    internal class XmlResourceHelper
    {
        // Fields
        private static readonly string CONFIG_FILE_PATH = @"ConfigFile\";
        private static readonly string LOAD_XML_FROM_RESX = "LoadXmlFromResx";
        private static readonly string RESX_ASSEMBLY_DLL = "ResxAssemblyDll";
        private static readonly string RESX_FULL_NAME = "{0}.ConfigFile.{1}";

        // Methods
        private XmlDocument createBitmapFromResources(Assembly assembly, string fileName)
        {
            Assembly assembly2 = assembly;
            XmlDocument document = new XmlDocument();
            Stream manifestResourceStream = assembly2.GetManifestResourceStream(fileName);
            if (manifestResourceStream == null)
            {
                throw new APPException(string.Format("文件名:{0} 在资源{1}中找不到,请确认是否已经添加该文件作为嵌入资源?", fileName, assembly2.FullName));
            }
            string xml = new StreamReader(manifestResourceStream, Encoding.UTF8).ReadToEnd();
            document.LoadXml(xml);
            return document;
        }

        public XmlDocument CreateXmlDocument(string baseDir, string xmlFileName)
        {
            string str = ConfigurationManager.AppSettings[LOAD_XML_FROM_RESX];
            if (string.Compare(Path.GetExtension(xmlFileName), ".XML", true) != 0)
            {
                xmlFileName = xmlFileName + ".xml";
            }
            bool flag = false;
            if (!string.IsNullOrEmpty(str))
            {
                flag = Convert.ToBoolean(str);
            }
            XmlDocument mapping = null;
            if (!flag)
            {
                xmlFileName = baseDir + xmlFileName;
                if (!File.Exists(xmlFileName))
                {
                    throw new APPException(string.Format("XML 文件{0}不存在", xmlFileName), APPMessageType.SysErrInfo);
                }
                string str2 = xmlFileName;
                if (CacheProxy.ContainsXmlCfgFile(str2))
                {
                    return CacheProxy.GetCacheXmlCfgFile<XmlDocument>(str2);
                }
                mapping = new XmlDocument();
                mapping.Load(xmlFileName);
                CacheProxy.CacheXmlCfgFile(str2, mapping);
                return mapping;
            }
            string str3 = ConfigurationManager.AppSettings[RESX_ASSEMBLY_DLL];
            if (string.IsNullOrEmpty(str3))
            {
                throw new APPException(string.Format("请配置XML 资源文件所属的配件名称{0}", RESX_ASSEMBLY_DLL));
            }
            Assembly assembly = Assembly.LoadWithPartialName(str3);
            if (assembly == null)
            {
                throw new APPException(string.Format("请检查配件{0} 是否存在", str3));
            }
            xmlFileName = string.Format(RESX_FULL_NAME, str3, xmlFileName);
            string key = xmlFileName;
            if (CacheProxy.ContainsXmlCfgFile(key))
            {
                return CacheProxy.GetCacheXmlCfgFile<XmlDocument>(key);
            }
            mapping = this.createBitmapFromResources(assembly, xmlFileName);
            CacheProxy.CacheXmlCfgFile(key, mapping);
            return mapping;
        }
        public XmlDocument CreateXmlDocument(string xmlFileName)
        {
            return this.CreateXmlDocument(CommonHelper.GetApplicationDirectory() + CONFIG_FILE_PATH, xmlFileName);
        }
    }
}
